package com.fjd.controller;

import com.fjd.dto.OrderAddressEditDTO;
import com.fjd.dto.OrderDTO;
import com.fjd.dto.OrderSubmitDTO;
import com.fjd.dto.OrdersPaymentDTO;
import com.fjd.entity.DeliveryDetailHistory;
import com.fjd.result.Result;
import com.fjd.service.DeliveryService;
import com.fjd.service.OrderService;
import com.fjd.vo.AddressVO;
import com.fjd.vo.GoodVO;
import com.fjd.vo.OrderPaymentVO;
import com.fjd.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shopping/order")
@Slf4j
@Api(tags = "订单管理")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private DeliveryService deliveryService;

    /**
     * 根据状态查询相关订单
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据状态查询相关订单")
    public Result<List<OrderVO>> list(OrderDTO orderDTO){
        List<OrderVO> list = orderService.list(orderDTO);
        return Result.success(list);
    }

    //获取订单详细信息
    @GetMapping("/select")
    @ApiOperation("查询某一订单")
    public Result<Map<String, Object>> select(String orderId) {
        // 获取订单信息
        OrderVO orderVO = orderService.select(orderId);
        // 获取地址信息
        AddressVO addressVO = orderService.getOrderAddr(orderVO.getAddressId());
        //获取物流信息
        List<DeliveryDetailHistory> deliveryDetailHistoryList=deliveryService.getDeliveryDetail(orderId);

        // 使用 Map 封装数据
        Map<String, Object> result = new HashMap<>();
        result.put("order", orderVO);
        result.put("address", addressVO);
        result.put("deliver",deliveryDetailHistoryList);

        // 返回给前端
        return Result.success(result);
    }

    @PostMapping("/submit")
    @ApiOperation("商城直接提交订单")
    public Result<List<GoodVO>> submit(@RequestBody List<OrderSubmitDTO> orderSubmitDTOList){
        List<GoodVO> goodVOList = orderService.submit(orderSubmitDTOList);
        return Result.success(goodVOList);
    }


    @GetMapping("/cancel/{orderId}")
    @ApiOperation("取消订单")
    public Result cancelOrder(@PathVariable String orderId){
        orderService.cancelOrder(orderId);
        return Result.success();
    }

    @GetMapping("/address")
    public Result addressEdit(OrderAddressEditDTO orderAddressEditDTO){
        orderService.editAddress(orderAddressEditDTO);
        return Result.success();
    }

    @GetMapping("/again/{orderId}")
    @ApiOperation("再来一单")
    public Result<List<GoodVO>> again(@PathVariable String orderId){
        List<GoodVO> goodVOList = orderService.again(orderId);
        return Result.success(goodVOList);
    }

    @GetMapping("/{orderId}")
    @ApiOperation("获取订单详细信息")
    public Result<OrderVO> orderDetail(@PathVariable String orderId){
        OrderVO orderVO = orderService.detail(orderId);
        return Result.success(orderVO);
    }

    @DeleteMapping("/{orderId}")
    @ApiOperation("删除订单")
    public Result deleteOrder(@PathVariable String orderId){
        orderService.deleteOrder(orderId);
        return Result.success();
    }

    @GetMapping("/confirm/{orderId}")
    @ApiOperation("确认收货")
    public Result confirmOrder(@PathVariable String orderId){
        orderService.confirmOrder(orderId);
        return Result.success();
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    @GetMapping("/delivery/{orderId}")
    @ApiOperation("获取订单物流信息")
    public Result<List<DeliveryDetailHistory>> delivery(@PathVariable String orderId){
        List<DeliveryDetailHistory> deliveryDetailHistories = orderService.deliverydetail(orderId);
        return Result.success(deliveryDetailHistories);
    }

    @GetMapping("/pay/{orderId}")
    @ApiOperation("支付订单（仅演示用）")
    public Result payOrder(@PathVariable String orderId){
        orderService.payorder(orderId);
        return Result.success();
    }
}
